﻿using Iworld.Backstage.Framework;
using Iworld.Backstage.Framework.AdministratorService;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Backstage
{
    public partial class EditAdministratorWindow : ChildWindow
    {
        public EditAdministratorWindow()
        {
            InitializeComponent();
            this.Loaded += EditAdministratorWindow_Loaded;
        }

        void EditAdministratorWindow_Loaded(object sender, RoutedEventArgs e)
        {
            AdministratorServiceClient client = new AdministratorServiceClient();
            client.GetAdministratorGroupsCompleted += client_GetAdministratorGroupsCompleted;
            client.GetAdministratorGroupsAsync(new GetAdministratorGroupsImport()
            {
                PageIndex = 1,
                Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
            });
        
        }

        void client_GetAdministratorGroupsCompleted(object sender, GetAdministratorGroupsCompletedEventArgs e)
        {
            if (e.Result.Success)
            {
                int count = -1;
                int selectIndex = -1;
                foreach (var k in e.Result.List)
                {
                    count++;
                    groups.Add(new FilterBoxModel(k.Name, k.Id, null));
                    if(Info.Group.Id == k.Id)
                    {
                        selectIndex = count;
                    }
                } 
                UIGroup.ItemsSource = groups;
                UIGroup.SelectedIndex = selectIndex;
            }
            else
            {
                groups.Add(new FilterBoxModel("加载用户组失败....", null, null));
            }
        }

        ObservableCollection<FilterBoxModel> groups = new ObservableCollection<FilterBoxModel>();

        public AdministratorExport Info
        {
            get { return (AdministratorExport)GetValue(InfoProperty); }
            set { SetValue(InfoProperty, value); }
        }


        public static readonly DependencyProperty InfoProperty =
            DependencyProperty.Register("Info", typeof(AdministratorExport), typeof(EditAdministratorWindow), new PropertyMetadata(null, (s, a) => {
                EditAdministratorWindow win = s as EditAdministratorWindow;
                AdministratorExport ae = a.NewValue as AdministratorExport;
                win.UIAccount.Text = ae.Account;
                win.UIUsername.Text = ae.Username;
            }));

        

        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Enter(object sender, RoutedEventArgs e)
        {
            FilterBoxModel fb = UIGroup.SelectedItem as FilterBoxModel;
            if (fb == null || fb.Value == null)
            {
                new ErrorPrompt() { Info = "请选择管理员组" }.Show();
                return;
            }
            ConfirmPrompt cp = new ConfirmPrompt() { Info = "确认修改吗?" };
            cp.Closed += (s, args) =>
            {
                if (cp.DialogResult == true)
                {
                    AdministratorServiceClient client = new AdministratorServiceClient();
                    client.EditAdministratorCompleted += client_EditAdministratorCompleted;
                    client.EditAdministratorAsync(new EditAdministratorImport() {
                        Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token,
                        Account = UIAccount.Text,
                        NewPassword = UINewPassword.Password,
                        Username = UIUsername.Text,
                        GroupId = (int)fb.Value, Id=Info.Id
                    });
                }
            };
            cp.Show();
        }

        void client_EditAdministratorCompleted(object sender, EditAdministratorCompletedEventArgs e)
        {
            if (e.Result.Success)
            {
                new ErrorPrompt() { Info = "编辑成功" }.Show();
                this.DialogResult = true;
            }
            else
            {
                new ErrorPrompt() { Info = e.Result.Error }.Show();
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

